package cn.itsource.cw.service.dept;

import cn.itsource.cw.entity.TbDept;
import cn.itsource.cw.mapper.DeptMapper;
import cn.itsource.cw.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class DeptServiceImpl {
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 功能说明 保存dept信息
     *
     * @param dept
     * @return void
     * @author caiwen
     * @date 2020/7/22
     */
    public void insertDept(TbDept dept) {
        Date now = new Date();
        if (dept.getCreateDate() == null) {
            dept.setCreateDate(now);
            dept.setUpdateDate(now);
        }
        deptMapper.insertSelective(dept);
    }

    /**
     * 功能说明 批量添加
     *
     * @param depts
     * @return void
     * @author caiwen
     * @date 2020/7/22
     */
    public void insertDeptList(List<TbDept> depts) {
        deptMapper.insertList(depts);
    }

    /**
     * 功能说明 更新部门信息
     *
     * @param dept
     * @return void
     * @author caiwen
     * @date 2020/7/22
     */
    public void updateDept(TbDept dept) {
        dept.setUpdateDate(new Date());
        deptMapper.updateByPrimaryKeySelective(dept);
    }

    /**
     * 功能说明 分页查询
     *
     * @param param
     * @param pageNum
     * @param pageSize
     * @return cn.itsource.cw.vo.PageResult<cn.itsource.cw.entity.TbDept>
     * @author caiwen
     * @date 2020/7/22
     */
    public PageResult<TbDept> findPageByParam(TbDept param, int pageNum, int pageSize) {
        PageResult<TbDept> pageResult = new PageResult<>();

        Example example = new Example(TbDept.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(param.getName())) {
            //模糊查询
            criteria.andLike("name", "%" + param.getName() + "%");
        }
        // TODO 按日期查询
        //按更新时间排序
        example.orderBy("updateDate").desc();
        //设置分页
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<TbDept> pageDept = new PageInfo<>(deptMapper.selectByExample(example));
        if (pageDept.getSize() > 0) {
            pageResult.setDataList(pageDept.getList());
            pageResult.setPageNum(pageDept.getPageNum());
            pageResult.setTotalCount(pageDept.getTotal());
            pageResult.setTotalPage(pageDept.getPages());
        }
        return pageResult;
    }

    /**
     * 功能说明 根据主键查询
     *
     * @param id
     * @return cn.itsource.cw.entity.TbDept
     * @author caiwen
     * @date 2020/7/22
     */
    public TbDept findDeptById(Integer id) {
        return deptMapper.selectByPrimaryKey(id);
    }

    public TbDept findDeptByName(String deptName){
        return deptMapper.findDeptByName(deptName);
    }
}
